/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javamail.web;

import javamail.web.util.eContentTypes;
import javamail.web.util.eGenMessage;
import javamail.web.util.eGenOption;

/**
 *
 * @author sriram
 */
public class httpGenerator {
    String serverInfo = " Sriram 1.0";
    String httpVersion = "HTTP/1.1 ";
    String responseCode = null;
    String headerOpts[] = null;
    String CRLF = "\r\n";

    public httpGenerator() {
        // Default response is to send 200 OK
        responseCode = new String(httpVersion + eGenMessage.OK.toString() + CRLF);
        
        // Allocate memory and initialize the arrays
        headerOpts = new String[eGenOption.MAXOPT.toInt()];
        for(int i=0; i<eGenOption.MAXOPT.toInt();i++) 
            headerOpts[i] = "";
    }
    
    // Not necessary to call this, unless you want to change 
    // response for some reason
    public void add200OK(){
        responseCode = httpVersion + eGenMessage.OK.toString() + CRLF;
    }
    
    public void add304NotModified(){
        responseCode = httpVersion + eGenMessage.NM.toString() + CRLF;
    }
    
    public void add404NotFound(){
        responseCode = httpVersion + eGenMessage.NF.toString() + CRLF;
    }
    
    public void addServer() {
        headerOpts[eGenOption.SERVER.toInt()] = eGenOption.SERVER.toString() + serverInfo + CRLF;
    }
    
    public void addContentType(String fileName) {
        headerOpts[eGenOption.CONTENT_TYPE.toInt()] = eGenOption.CONTENT_TYPE.toString()
            + " " + eContentTypes.getType(fileName) + CRLF;
    }
    
    public String getResponseHeader() {
        String result = null;
        // Add the http version and response code
        result = new String(responseCode);
        // Add all the options that were populated
        for(int i=0; i<eGenOption.MAXOPT.toInt(); i++)
            if((headerOpts[i] != null) && !headerOpts[i].equals(""))
                result += headerOpts[i];
        result += CRLF;
        //System.out.println(result);
        return result;
    }
    
    public static String getNotFoundResponse() {
      return "<html>\r\n" +
                "<head>\r\n" +
                    "<title>404 NOT FOUND</title>\r\n" +
                "</head>\r\n" +
                "<body>\r\n" +
                    "<p>File not Found!!</p>\r\n" +
                "</body>\r\n" + 
             "</html>\r\n";
    }
    
    public static String inputInvalidResponse(String err) {
      return "<html>\r\n" +
                "<head>\r\n" +
                    "<title>Error Occured!</title>\r\n" +
                "</head>\r\n" +
                "<body>\r\n" +
                    "<p>" + err + "</p>\r\n" +
                "</body>\r\n" + 
             "</html>\r\n";
    }

    public static String successResponse() {
      return "<html>\r\n" +
                "<head>\r\n" +
                    "<title>MAIL SENT!</title>\r\n" +
                "</head>\r\n" +
                "<body>\r\n" +
                    "<p>The mail was sent successfully.</p>\r\n" +
                "</body>\r\n" + 
             "</html>\r\n";
    }

    public static String successResponse(String str) {
      return "<html>\r\n" +
                "<head>\r\n" +
                    "<title>Command Successful!</title>\r\n" +
                "</head>\r\n" +
                "<body>\r\n" +
                    "<p>" + str + "</p>\r\n" +
                    "</body>\r\n" + 
             "</html>\r\n";
    }
    
    public static String queryPage() {
        String currentMsg = javamail.main.glblConfig.getMessageText();
        if(currentMsg==null || currentMsg.trim().equals(""))
            currentMsg = "CURRENTLY NO MESSAGE EXISTS";
        return "<html>\r\n"+
                "<head>"+
                "<title>SIPSPEAKER - VOICE MAILBOX</title>\r\n"+
                "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n"+
                "</head>\r\n"+
                "<body>\r\n"+
                "<form name=\"query\" action=\"action.sss\" method=\"POST\">\r\n"+
                "<table>\r\n"+
                "<tr>\r\n"+
                  "<td>Current Message:</td>\r\n"+
                  "<td><p>"+currentMsg+"</p></td>\r\n"+
                  "<td><input type=\"submit\" value=\"Delete Message\" name=\"delete\" /></td>\r\n"+
                "</tr>\r\n"+
                "<tr>\r\n"+
                  "<td>New Message:</td>\r\n"+
                  "<td><input type=\"text\" name=\"new\" value=\"\" size=\"60\" /></td>\r\n"+
                  "<td><input type=\"submit\" value=\"Update Message\" name=\"update\" /></td>\r\n"+
                "</tr>\r\n"+
              "</form>\r\n"+
              "</body>\r\n"+
            "</html>\r\n";
    }

}
